﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;


using Meqs.Entity;
using Meqs.Logic.Interface;
using Castle.Services.Transaction;
using log4net;

namespace Meqs.Logic.Implement
{
    [Transactional]
    public class ExamDetailSchoolLogic : CommonLogic<ExamDetailSchool, int>, IExamDetailSchoolLogic
    {
        [Transaction(TransactionMode.RequiresNew)]
        public void GetPosition(ref int classP, ref int gradeP,  Hashtable ht)
        {
            try
            {
                string statementName = "ExamDetailSchool.FindStudentPositionNum";
                classP = ExecuteQueryForObject<int>(statementName, ht);
                ht.Remove("ClsID");
                gradeP = ExecuteQueryForObject<int>(statementName, ht);
                
            }
            catch (Exception ex)
            {
                ILog _log = LogManager.GetLogger("DatabaseOp");
                _log.Error("更新对象出错：" + ex.Message, ex);
                throw;
            }
        }

        [Transaction(TransactionMode.RequiresNew)]
        public int SaveStudentScores(int[] stuids, int[] subjectids, double[] scores, int emid, int clsType,int grd,int clsid, string schoolmark)
        {

            List<ExamDetailSchool> list = new List<ExamDetailSchool>();
            string ids = "(0";
            for (int i = 0; i < stuids.Length; i++)
            {
                ids += "," + stuids[i];
                for (int j = 0; j < subjectids.Length; j++)
                {
                    ExamDetailSchool ge = new ExamDetailSchool();
                    ge.StudentID = stuids[i];
                    ge.SubjectID = subjectids[j];
                    ge.ExamID = emid;
                    ge.SchoolMark = schoolmark;
                    ge.Score = null;
                    ge.ClsID = clsid;
                    if (scores[i * subjectids.Length + j] != -1)
                    {
                        ge.Score = scores[i * subjectids.Length + j];
                    }
                    list.Add(ge);
                }
            }
            ids += ")";
            
            Hashtable table = new Hashtable();
            table.Add("SchoolMark", schoolmark);
            table.Add("ExamID", emid);
            table.Add("Studentid", ids);
            table.Add("ClsID", clsid);
            DeleteOneClassScoreForExam(table);

            foreach (ExamDetailSchool gs in list)
            {
                Create(gs);
            }

            ExamClassSchool eclass = new ExamClassSchool();
            eclass.SchoolClassID = clsid;
            eclass.ExamID = emid;
            eclass.ClassType = clsType;
            eclass.Grade = grd;
            eclass.SchoolMark = schoolmark;


            table = new Hashtable();
            table.Add("SchoolClassID", clsid);
            table.Add("ExamID", emid);
            table.Add("ClassType", clsType);
            table.Add("Grade", grd);
            table.Add("SchoolMark", schoolmark);
            IList<ExamClassSchool> tc = FindCustomer<ExamClassSchool>("ExamClassSchool.Find", table);
            if (tc.Count == 0)
            {
                ExecuteInsert("ExamClassSchool.Insert", eclass);
            }
            table = new Hashtable();
            table.Add("TestID", emid);
            table.Add("SchoolMark", schoolmark);
            try
            {
                string statementName = "ExamDetailSchool.UpdateCatch";
                ExecuteUpdate(statementName, table);

                table = new Hashtable();
                table.Add("ClassID", clsid);
                table.Add("SchoolMark", schoolmark);
                statementName = "ExamTeachSchool.FindCount";
                int count = ExecuteQueryForObject<int>(statementName, table);
                if (count <= 0)
                {
                    table = new Hashtable();
                    table.Add("ClsID", clsid);
                    statementName = "TeachClsCause.Find";
                    IList<TeachClsCause> l = ExecuteQueryForList<TeachClsCause>(statementName, table);
                    foreach (TeachClsCause t in l)
                    {
                        ExamTeachSchool et = new ExamTeachSchool(0, emid, t.CauseID, t.TeachID, clsid, schoolmark);
                        statementName = "ExamTeachSchool.Insert";
                        ExecuteInsert(statementName, et);
                    }
                }
            }
            catch (Exception ex)
            {
                ILog _log = LogManager.GetLogger("DatabaseOp");
                _log.Error("更新对象出错：" + ex.Message, ex);
                throw;
            }
            //return BatchSave<ExamDetailSchool>("ExamDetailSchool.Insert", list);
            return 0;
        }

        private int DeleteOneClassScoreForExam(Hashtable ht)
        {
            try
            {
                string statementName = "ExamDetailSchool.DeleteOneClassScoreForExam";
                return ExecuteDelete(statementName, ht);
            }
            catch (Exception ex)
            {
                ILog _log = LogManager.GetLogger("DatabaseOp");
                _log.Error("删除全部对象出错：" + ex.Message, ex);
                throw;
            }
        }
        #region Implementation of ICommonLogic<ExamDetailSchool, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(ExamDetailSchool entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(ExamDetailSchool entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public ExamDetailSchool FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<ExamDetailSchool> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<ExamDetailSchool> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion
    }
}